package ar.edu.unq.mongodbexample.tests.persitence;

import java.util.ArrayList;
import java.util.List;

import ar.edu.unq.mongodbexample.homes.CareerHome;
import ar.edu.unq.mongodbexample.homes.SubjectHome;
import ar.edu.unq.mongodbexample.model.Career;
import ar.edu.unq.mongodbexample.model.Subject;
import ar.edu.unq.mongodbexample.persistence.MongoDBManager;
import junit.framework.TestCase;

public class CareerHomeTestCase extends TestCase {
	CareerHome home;
	List<Career> careers;
	
	public void setUp(){
		home=new CareerHome();
		careers=createCareers();
	}
	public void tearDown(){
		MongoDBManager.getInstance().getDataStore().getCollection(Career.class).drop();
		MongoDBManager.getInstance().getDataStore().getCollection(Subject.class).drop();
	}
	
	public void testSaveAndGetById(){
		Career career=new Career(6,"Administracion Hotelera");
		home.save(career);
		assertEquals(6, home.findById("Administracion Hotelera").getAverageDuration());
	}
	
	public void testSaveAndGetCareerByIdAndGetReferences(){
		SubjectHome subjectHome=new SubjectHome();
		Subject subject=new Subject("Estrategias de Persistencia");
		subjectHome.save(subject);
		Career career=new Career(1,"Tecnicatura en Programacion Informatica");
		career.addSubject(subject);
		home.save(career);
		
		Career careerFound=home.findById("Tecnicatura en Programacion Informatica");
		assertEquals(1, careerFound.getAverageDuration());
		assertEquals(subject.getName(), careerFound.getSubject().get(0).getName());
	}
	
	public void testSaveAllAndGetAllStudents() {
    	home.saveAll(careers);
    	List<Career> careerList= home.getAll();
        assertEquals(careerList.size(), 4);
    }
	
	public List<Career> createCareers(){
		List<Career> careerList=new ArrayList<Career>();
		careerList.add(new Career(1,"Tecnicatura en Programacion Informatica"));
		careerList.add(new Career(2,"Terapia Ocupacional"));
		careerList.add(new Career(3,"Ingenieria Naval"));
		careerList.add(new Career(4,"Licenciatura en Biotecnologia"));
        return careerList;
	}
}
